package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.stat.model.StatCoreBehaviorModel;
import com.starbaba.stat.model.StatUserCoreModel;
import com.starbaba.stat.service.StatUserCoreService;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class StatUserCoreAction extends BaseAction implements ModelDriven<StatUserCoreModel>{

	private StatUserCoreModel model = null;
	
	private StatUserCoreService service;
	
	private InputStream input;

	private String fileName;
	
	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	@Autowired
	@Qualifier("statUserCoreService")
	public void setService(StatUserCoreService service) {
		this.service = service;
	}
	
	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		String value = service.getExportString(pageResponse, paramsMap);
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}

	public String showPage() {
		try {
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			service.showPage(paramsMap);
		} catch (Exception e) {
			LogService.error("用户核心统计异常(showPage) ", e);
		}
		return ActionSupport.SUCCESS;
	}
	
	public void queryList() {
		try {
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			parsePageRequest();
			PageResponse pageResponse = new PageResponse();
			int start = pageRequest.getIDisplayStart();
			int length = pageRequest.getIDisplayLength();
			List<StatCoreBehaviorModel> list = new ArrayList<StatCoreBehaviorModel>();
			list = service.getList(start, length, pageResponse, paramsMap);

			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());

			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			LogService.error("用户核心统计(queryList)异常: ", e);
		}
	}

	@Override
	public StatUserCoreModel getModel() {
		if (model == null) {
			model = new StatUserCoreModel();
		}
		return model;
	}

}
